
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:更新绑定手机协议类
 *
 * @author jerryliu
 * @since 2015年8月3日 上午9:51:45
 */
public class UpdateBindPhoneTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public UpdateBindPhoneResponse request(String userName, String mobilePhone,
            String smsCheckCode, String unixtime, String token) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        UpdateBindPhoneResponse respObj = new UpdateBindPhoneResponse();
        ResponsePackage resp = new BindPhoneResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_BIND_PHONE_UPDATE);
        params.put("username", UrlEncodeUtil.encode(userName, "utf-8"));
        params.put("mobile", mobilePhone);
        params.put("checkcode", smsCheckCode);
        params.put("unixtime", unixtime);
        params.put("flowtoken", token);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_BIND_PHONE_UPDATE);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class BindPhoneResponsePackage extends HttpBaseResponsePackage<UpdateBindPhoneResponse> {
        @Override
        protected void handleResponse(UpdateBindPhoneResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "UpdateBindPhoneTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_BIND_PHONE_UPDATE.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            response.setNickname(jsonResult.optString("nickname"));
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

    public static class UpdateBindPhoneResponse extends ResponseMessage {
        private String nickname;

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
    }
}
